import {ByteRange_Pro, IsIP_Pro, IsIP, IPCompareLower} from "@/utils/utils"

export const addSchema: FormSchema = {
    "style": {
        "width": "auto"
    },
    "formItemLayout": {
        "labelCol": {
            "span": 6
        },
        "wrapperCol": {
            "span": 18
        }
    },
    "watchKeys": [ // 需要监听字段，值为string数组
        "localport", "localendport","externalipstr","externalendipstr"
    ],
    "watchCallback": (watchKeys, {dynamicForm, modelRef, validateInfos}) => { // watchKeys监听的字段对应的值变化后执行的回调
        const {localport, localendport,externalipstr,externalendipstr} = modelRef
        console.log(watchKeys, 'watchKeys')
        console.log(modelRef, 'modelRef表单中所有的字段和值，类型：object')
        if (localendport < localport||!localendport) {
            modelRef.localendport = modelRef.localport
        }
        if(!IsIP(externalipstr)&&!IsIP(externalendipstr)){
            modelRef.externalendipstr = modelRef.externalipstr
        } else if(IsIP(externalendipstr)&&(externalipstr&&IPCompareLower(externalendipstr,externalipstr))){
            validateInfos["externalendipstr"].validateStatus='error'
            validateInfos["externalendipstr"].help="结束IP不能小于起始IP"
        }
    },
    "formItem": [
        {
            key: "ruletype",
            value: 0,
            hidden: true,
        },
        {
            "type": "input",
            "label": "名称",
            "key": "name",
            "value": "",
            "props": {
                "placeholder": "规则名称"
            },
            "rules": [
                {
                    "required": true,
                    "message": "规则名称不能为空"
                },
                {
                    "validator":(rule,value,callback)=>{return ByteRange_Pro(rule,value,callback,32)},
                    "message": "策略名称最多32个字节"
                }
            ]
        },
        {
            "type": "select",
            "label": "协议",
            "key": "protocol",
            "value": 0,
            "options": [
                {
                    "value": 0,
                    "label": "TCP"
                },
                {
                    "value": 1,
                    "label": "UDP"
                },
                // {
                //     "value": 2,
                //     "label": "TCP,UDP"
                // }
            ]
        },
        {
            "type": "inputNumRange",
            "label": "本地端口范围",
            "key": "localports",
            "variable": {
                start: "localport",
                end: "localendport"
            },
            "value": {
                start: "",
                end: ""
            },
            "placeholder": {
                start: "开始端口",
                end: "结束端口"
            },
            "rules": [
                {
                    "required": true,
                    "message": "端口号不能为空"
                },
                {
                    "type": 'number',
                    "min": 0,
                    "max": 65535,
                    "message": "端口号范围为0-65535"
                }
            ]
        },
        {
            "type": "inputRange",
            "label": "远程地址范围",
            "key": "ips",
            "variable": {
                start: "externalipstr",
                end: "externalendipstr"
            },
            "value": "",
            "placeholder": {
                start: "远程开始地址",
                end: "远程结束地址"
            },
            "rules": [
                {
                    "required": true,
                    "message": "网络地址不能为空"
                },
                {
                    "validator": IsIP_Pro
                }
            ]
        },
        // {
        //     "type": "inputNumRange",
        //     "label": "远程端口范围",
        //     "key": "outports",
        //     "variable": {
        //         start: "externalport",
        //         end: "externalendport"
        //     },
        //     "value": "",
        //     "placeholder": {
        //         start: "远程开始端口",
        //         end: "远程结束端口"
        //     },
        //     "rules": [
        //         {
        //             "required": true,
        //             "message": "端口号不能为空"
        //         },
        //         {
        //             "type": 'number',
        //             "min": 0,
        //             "max": 65535,
        //             "message": "端口号范围为0-65535"
        //         }
        //     ]
        // },
        {
            "type": "radio",
            "label": "是否禁止",
            "key": "using",
            "value": 0,
            "options": [
                {
                    "value": 1,
                    "label": "是"
                },
                {
                    "value": 0,
                    "label": "否"
                }
            ]
        },
    ]
}
